/* mtest.c
**
** Copyright (c) 2007, Meadhbh S. Hamrick
** All rights reserved.
** 
** Redistribution  and  use  in  source  and  binary  forms,  with  or  without
** modification, are permitted provided that the following conditions are met:
** 
**  * Redistributions of  source code must  retain the above  copyright notice,
**    this list of conditions and the following disclaimer.
** 
**  * Redistributions in binary form must reproduce the above copyright notice,
**    this list of conditions and the following disclaimer in the documentation
**    and/or other materials provided with the distribution.
** 
**  * Neither the  name of  Project Meadhbh  nor the names of  its contributors
**    may be used  to endorse  or promote  products derived from this  software
**    without specific prior written permission.
** 
** THIS SOFTWARE IS PROVIDED BY  THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY  EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT  LIMITED TO, THE
** IMPLIED WARRANTIES  OF MERCHANTABILITY AND FITNESS FOR  A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO  EVENT SHALL THE  COPYRIGHT OWNER OR  CONTRIBUTORS BE
** LIABLE  FOR  ANY  DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
** CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT  LIMITED  TO,  PROCUREMENT  OF
** SUBSTITUTE GOODS  OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN
** CONTRACT,  STRICT LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE OR  OTHERWISE)
** ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
** 
** $Id: mtest.c 76 2009-08-03 16:54:02Z meadhbh.siobhan $
*/

/** \file mtest.c
 ** This file contains a simple CUNIT test harness.
 */

/* Macro Definitions */
#define MUTIL_KERNEL

/* File Includes */
#include "mtest.h"
#include "mutil.h"

/* Typedefs, Structs, Unions, Enums, etc. */

/* Static Function Prototypes */

extern CU_SuiteInfo mu_suites[];

/* Static Variable Declarations */

static char *mu_local_sourceid = "$Id: mtest.c 76 2009-08-03 16:54:02Z meadhbh.siobhan $";

/* Function Definitions */

/** \fn int mu_cpp_find( char *array[], char *trial, unsigned int size)
**  \brief Find the index of an a string in an array of strings
**  \param array an array of character pointers
**  \param trial pointer to the string to find
**  \param size size of the array pointed to by array
**  \return the index of the trial string in the array
*/
tTestErr mu_test_new( int argc, char *argv[] ) {
  tTestErr err = TEST_E_NOERR;
  int iTestTypeFound;
  static char *cpp_testTypes[] = {
    "automated",
    "basic",
    "console",
    "curses",
    NULL
  };

  do {
    if( argc <= 1 ) {
      App.testType = TEST_T_AUTOMATED;
    } else {
      iTestTypeFound = mu_cpp_find( cpp_testTypes, argv[1], 4 );

      if( -1 == iTestTypeFound ) {
	err = TEST_E_USAGE;
	break;
      }
      App.testType = (tTestType) iTestTypeFound;
    }

    if( CUE_SUCCESS != CU_initialize_registry() ) {
      err = TEST_E_INIT;
      break;
    }


    if( CUE_SUCCESS != CU_register_suites( mu_suites ) ) {
      err = TEST_E_INIT;
      break;
    }

  } while( 0 );

  if( TEST_E_USAGE == err ) {
    fprintf( stderr, "Usage:\n   %s <test type>\nwhere <test type> is one of:\n   automated\n   basic\n   console\n   curses\n", argv[0] );
  }

  return( err );
}

tTestErr mu_test_do() {
  tTestErr err = TEST_E_NOERR;

  do {
    switch( App.testType ) {
    case TEST_T_AUTOMATED:
      CU_automated_run_tests();
      break;

    case TEST_T_BASIC:
      CU_basic_run_tests();
      break;

    case TEST_T_CONSOLE:
      CU_console_run_tests();
      break;

    case TEST_T_CURSES:
      /* todo: figure out how to properly add curses functionality */
      /*      CU_curses_run_tests(); */
      break;

    default:
      err = TEST_E_BADPARAM;
      break;
    }
  } while( 0 );

  return( err );
}

int main( int argc, char *argv[] ) {
  tTestErr err = TEST_E_NOERR;

  do {
    if( TEST_E_NOERR != ( err = mu_test_new( argc, argv ) ) ) break;
    if( TEST_E_NOERR != ( err = mu_test_do() ) ) break;
  } while( 0 );

  return( ( (int) err ) & 0xFF );
}

